package com.example.swagger.mapper;

import com.example.swagger.entity.t_userHandle;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface t_userHandleMapper {

    /**
     * 插入记录，手动分配主键
     *
     * @param user
     * @return
     */
    @Insert("INSERT INTO t_userHandle (id, username, passwd) VALUES (#{id}, #{username}, #{passwd})")
    int addUserAssignKey(t_userHandle user);

    /**
     * 插入记录，数据库生成主键
     *
     * @param user
     * @return
     */
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("INSERT INTO t_userHandle (username, passwd) VALUES (#{username}, #{passwd})")
    int addUserGeneratedKey(t_userHandle user);

    /**
     * 插入记录，选择主键
     *
     * @param user
     * @return
     */
    @Insert("INSERT INTO t_userHandle (username, passwd) VALUES (#{username}, #{passwd})")
    @SelectKey(statement = "SELECT UNIX_TIMESTAMP(NOW())", keyColumn = "id", keyProperty = "id", resultType = Long.class, before = true)
    int addUserSelectKey(t_userHandle user);

    /**
     * 删除记录
     *
     * @param id
     * @return
     */
    @Delete("DELETE FROM t_userHandle WHERE id=#{id}")
    int delete(Long id);

    /**
     * 修改记录
     *
     * @param user
     * @return
     */
    @Update("UPDATE t_userHandle SET username=#{username}, passwd=#{passwd} WHERE id=#{id}")
    int update(t_userHandle user);

    @Select("SELECT * FROM t_userHandle")
    public List<t_userHandle> loadUsersByIdHandAlias();

    /**
     *  根据ID查询，开了自动驼峰转换
     *
     * @param id
     * @return
     */
    @Select("SELECT * FROM t_userHandle WHERE id=#{id}")
    public List<t_userHandle> loadByIdAutoAlias(Long id);

    /**
     * 使用ResultMap
     *
     * @param id
     * @return
     */
    @Results(id = "userMap", value = {
            @Result(id=true, column = "id", property = "id"),
            @Result(column = "username", property = "username"),
            @Result(column = "passwd", property = "passwd"),
            @Result(column = "birth_day", property = "birthDay")
    })
    @Select("SELECT * FROM t_userHandle WHERE id=#{id}")
    t_userHandle loadByIdResultMap(Long id);

    /**
     * 引用其他的Result
     *
     * @param id
     * @return
     */
    @ResultMap("userMap")
    @Select("SELECT * FROM t_userHandle WHERE id=#{id}")
    t_userHandle loadByIdResultMapReference(Long id);
}
